using Backend;
//FEN 2008.10.28 Using generics
/// <summary>
/// Represents a vertex in a directed graph
/// </summary>
using System.Collections.Generic;

namespace BusinessTier
{
    public class Vertex
    {
        private string _name;
        private bool _mark;
        private IList<Edge> _edgeList;

        // information for DijkstraAlgorithm
        private Vertex previousVertex; //the vertex from which we can get here on the cheapest price
        private double priceToGetHere; // the price to get to this vertex from the starting vertex
 


        public Vertex(string name)
        {
            _mark = false;
            this._name = name;
            _edgeList = new List<Edge>();
        }
        public override string ToString()//For debugging purposes
        {
            return Name + "  " + Mark;
        }

        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }
        }

        public bool Mark
        {
            get
            {
                return _mark;
            }
            set
            {
                _mark = value;
            }
        }

        public IList<Edge> Edges() //must return a List in order to be able to remove edges
        {

            return _edgeList;
        }

        public void AddEdge(Vertex to, Leg leg)
        {

            //check if the edge already exists
            Edge edge = findEdgeTo(to);
            if (edge != null)
            {

                edge.addLeg(leg);
            }

           //create the edge if it doesn't exist
            else
            {
                Edge e = new Edge(this, to);
                e.addLeg(leg);
                _edgeList.Add(e);
            }
        }

        public int OutDegree()
        {
            return _edgeList.Count;
        }

        public void RemoveEdge(Edge e)
        {
            _edgeList.Remove(e);
        }


        //remove the given leg from the edge with the given vertex
        public void removeLeg(Vertex destination, Leg leg)
        {
            Edge edge = findEdgeTo(destination);


            if (edge != null)
            {
                edge.removeLeg(leg);

                if (edge.Legs.Count == 0)  //check if the edge contains anymore legs, if it doesn't- remove it
                    RemoveEdge(edge);
            }

        }


        // get the legs to the given vertex
        public List<Leg> getLegsTo(Vertex destination)
        {

            Edge edge = findEdgeTo(destination);

            if (edge != null)
                return edge.Legs;

            else
                return null;
        }

        // utility method which finds the edge to teh given vertex
        private Edge findEdgeTo(Vertex to)
        {
            Edge theEdge = null;

            //find the edge in the list of edges
            foreach (Edge edge in _edgeList)
            {
                if (edge.To._name.Equals(to.Name))
                    theEdge = edge;
            }

            return theEdge;
        }

        public Leg getLegWithLowestPrice(Vertex to)
        {
            Edge edge = findEdgeTo(to);

            if (edge != null)
                return edge.getLegWithLowestPrice();

            else
                return null; ;
           
        }


        public Vertex PreviousVertex
        {
            get { return previousVertex; }
            set { previousVertex = value; }
        }

        public double PriceToGetHere
        {
            get { return priceToGetHere; }
            set { priceToGetHere = value; }
        }


     
    }

}